package com.nciae.service.impl;

import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.nciae.service.IFastDFSClientService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@Service
public class FastDFSClientServiceImpl implements IFastDFSClientService {

    private static final String BASE_URL = "http://1.116.60.188:8888/";

    @Autowired
    private FastFileStorageClient fastFileStorageClient;

    @Override
    public String upload(MultipartFile file) {
        try {
            InputStream in = file.getInputStream();
            StorePath storePath = fastFileStorageClient.uploadFile(in, file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()), null);
            String fullPath = storePath.getFullPath();
            return BASE_URL + fullPath;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
